import { ref } from 'vue'

const loading = ref(0)
const padding = ref(false)

const setLoadingValue = (v) => loading.value = v

let timer = null

const loadingStart = () => {
  if (timer) {
    clearInterval(timer)
    loading.value = 0
  }
  padding.value = true
  // setLoadingValue(5)
  timer = setInterval(() => {
    if (loading.value >= 100) {
      clearInterval(timer)
      setLoadingValue(100)
    } else {
      setLoadingValue(loading.value += 1)
    }
  }, 100)
}

const loadingDone = () => {
  if (timer) clearInterval(timer)
  padding.value = false
  setLoadingValue(100)
  setTimeout(() => loading.value = 0, 500)
}

const done = () => !padding.value && loading.value === 100
const fail = () => padding.value && loading.value === 100

export {
  loading,
  padding,
  done,
  fail,
  loadingStart,
  loadingDone,
  setLoadingValue,
}